import { ILogger } from '@midwayjs/logger';
import { mkdir } from 'fs';
import path = require('path');

/*
 * @Author: 颜礼田 2564217790@qq.com
 * @Date: 2024-09-07 09:29:14
 * @LastEditors: 颜礼田 2564217790@qq.com
 * @LastEditTime: 2024-09-09 09:07:41
 * @FilePath: /webServer/src/plugins/folder.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
export function useChecked(app) {
  const logger: ILogger = app.getCoreLogger();
  logger.info('开始检查项目文件夹是否创建');
  mkdirFolder(
    path.join(__dirname, '../../', 'merge'),
    'pdf文件合并缓存文件夹',
    logger
  );
  mkdirFolder(
    path.join(__dirname, '../../', 'public'),
    '静态资源托管文件夹',
    logger
  );
}

function mkdirFolder(folderPath, folderName, logger: ILogger) {
  mkdir(path.join(folderPath), err => {
    if (err && (err.errno === -17 || err.errno === -4075)) {
      logger.info(`${folderName}已创建`);
    } else if (err) {
      logger.error('创建文件夹时发生错误:', err);
    }
  });
}
